/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filmlibrary.web;

import filmlibrary.orm.ORMHelper;
import filmlibrary.orm.classes.Groups;
import filmlibrary.orm.classes.Users;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author karol
 */
@ManagedBean
@ViewScoped
public class EditUserBean implements java.io.Serializable {
    private static final Logger logger = Logger.getLogger("filmlibrary.web.EditUserBean");
    private ORMHelper orm = new ORMHelper();
    private Users user = new Users();
    private int id=0;
    private String pass=null;    

    /** Creates a new instance of EditUserBean */
    public EditUserBean() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String Id = request.getParameter("id");

        if (Id != null ) {
            this.id = Integer.parseInt(Id);
        }
        if(this.id == 0)
        {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("users.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(EditBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        logger.info("konstrukor EditUser id usera: " + id);
        user = orm.getUserbyId(this.id);
        logger.info("konstruktor EditUser user: " + user);
                
        if(user == null ){
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("404");
            } catch (IOException ex) {
                Logger.getLogger(EditBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        Iterator<Groups> i = user.getGroupses().iterator();
        String login = new SessionBean().getLogin();
        while(i.hasNext()){
//            if(i.next().getGroupName().equals("ADMIN") && !user.getUsername().equals(login)){
            if(i.next().getGroupName().equals("ADMIN")){
                try {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("404");
                    break;
                } catch (IOException ex) {
                    Logger.getLogger(EditUserBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }
    public String editUser(){
        if(pass!=null){
            user.setPassword(Additional.MD5(pass));
        }
        orm.updateUser(user);
        return "users.xhtml?faces-redirect=true";
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
    
}
